﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Threading;
using wf = System.Windows.Forms;
using System.Windows.Forms;
using System.Reflection;
using System.IO;


namespace client.app
{
    public class MainGame : Game
    {
        public static MainGame Instance { get; private set; }
        public static comm.TimeStat UpdateTimeStat { get; private set; }
        public static comm.TimeStat DrawTimeStat { get; private set; }
        public static world.MainSpace MainSpace { get; private set; }
        public static world.UserInput UserInput { get; private set; }
        public static int Width { get; private set; }
        public static int Height { get; private set; }
        public static Rectangle ViewRect { get; private set; }
        public static RPackManager RPackManager { get; private set; }
        public static comm.DefineFile DefineFile { get; private set; }
        public static gui2.MainWindow MainWindow { get; private set; }
        public static bool Debug { get; set; }

        Queue<Action> _taskQ = new Queue<Action>();

        public MainGame(gui2.MainWindow mainWindow)
        {
            Thread.CurrentThread.IsBackground = true;

            IsMouseVisible = true;

            var graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = Conf.WIDTH;
            graphics.PreferredBackBufferHeight = Conf.HEIGHT;
            graphics.SynchronizeWithVerticalRetrace = true;
            //graphics.IsFullScreen = true;
            graphics.ApplyChanges();

            IsFixedTimeStep = false;

            Width = graphics.PreferredBackBufferWidth;
            Height = graphics.PreferredBackBufferHeight;
            ViewRect = new Rectangle(0, 0, Width, Height);

            MainWindow = mainWindow;

            var folder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../bin_data_define");
            DefineFile = new comm.DefineFile(folder);

            UpdateTimeStat = new comm.TimeStat();
            DrawTimeStat = new comm.TimeStat();

            Instance = this;
        }

        public void AddTask(Action callback)
        {
            _taskQ.Enqueue(callback);
        }

        protected override void OnExiting(object sender, EventArgs args)
        {
            comm.ThreadHelper.StopAll();
            MainWindow.Exit();
            base.OnExiting(sender, args);
        }
        protected override void LoadContent()
        {
            Content.RootDirectory = "client.conntent";
            RPackManager = new RPackManager();
            MainSpace = new world.MainSpace();
            UserInput = new world.UserInput();
            MainWindow.MainGameLoadContent();
        }
        protected override void Update(GameTime gameTime)
        {
            UpdateTimeStat.Update();

            while (_taskQ.Count > 0)
            {
                _taskQ.Dequeue()();
            }

            MainSpace.Update(UpdateTimeStat);
            UserInput.Update(UpdateTimeStat);

            base.Update(gameTime);
        }
        protected override void Draw(GameTime gameTime)
        {
            DrawTimeStat.Update();

            MainSpace.BeginDraw(DrawTimeStat);
            UserInput.BeginDraw(DrawTimeStat);

            GraphicsDevice.SetRenderTarget(null);

            MainSpace.Draw(DrawTimeStat);
            UserInput.Draw(DrawTimeStat);

            MainWindow.Draw();

            base.Draw(gameTime);
        }
    }
}
